package com.chennan.work.jiuqi.guochuangtuis;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chennan
 * @create 2024-01
 */
public class AnalysisCreateTable {
    private static String TABLENAME_FLAG_V24 = "_V24";
    private static String TABLENAME_FLAG_V23 = "_V23";
    public static void main(String[] args) {
        String readFled ="D:\\学习编程资料\\久其学习资料整理\\安徽一体化\\2024国创推送数据\\资产及决算系统报送表相关信息\\createtable";
        File file = new File(readFled);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            List<String> createTables = new LinkedList<>();
            List<TableDto> tableDtos = new LinkedList<>();
            for (File file1 : files) {
                dataRead(file1.getAbsolutePath(),createTables);
            }
//            System.out.println(createTables);
            for (String createTable : createTables) {
                String regex = "\".*?\"";
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(createTable.toString());
                TableDto tableDto = new TableDto();
                List<String> list = new LinkedList<>();
                while (matcher.find()) {
                    String group = matcher.group();
                    if(group.contains(TABLENAME_FLAG_V24)){
                        tableDto.setTableName(group.replace("\"",""));
                    }else {
                        list.add(group.replace("\"",""));
                    }
                }
                tableDto.setFileds(list);
                tableDtos.add(tableDto);
            }
           //生成insert select sql
            List<String> insertSql = new LinkedList<>();
            for (TableDto tableDto : tableDtos) {
                String tableName = tableDto.getTableName();
                if(StringUtils.isNotEmpty(tableName)){
                    List<String> fileds = tableDto.getFileds();
                    StringBuilder inserts = new StringBuilder();
                    inserts.append("INSERT INTO "+tableName+"(");
                    for (String filed : fileds) {
                        inserts.append(filed).append(",");
                    }
                    inserts.delete(inserts.length()-1,inserts.length());
                    inserts.append(")");

                    inserts.append("SELECT ");
                    for (String filed : fileds) {
                        inserts.append(filed).append(",");
                    }
                    inserts.delete(inserts.length()-1,inserts.length());
                    System.out.println(tableName);
                    inserts.append(" FROM "+tableName.replace(TABLENAME_FLAG_V24,"")+";");
                    insertSql.add(inserts.toString());
                }
            }
            System.out.println(insertSql);

        }
    }


    public static String dataRead(String filePath, List<String> createTables)  {
        StringBuilder data = new StringBuilder();
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        String regex = "CREATE TABLE .+?LOGGINGNOCOMPRESS";
        try {
            fileReader=new FileReader(filePath);
            bufferedReader = new BufferedReader(fileReader);
            String temp=null;
            while ((temp=bufferedReader.readLine())!=null){
                data.append(temp);
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(data.toString());
                if (matcher.find()) {
                    String group = matcher.group();
                    createTables.add(group.replace("LOGGINGNOCOMPRESS",""));
                    data.delete(0,data.length());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(Objects.nonNull(bufferedReader)){
                    bufferedReader.close();
                }
                if(Objects.nonNull(fileReader)){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data.toString();
    }

}
